package com.sec.android.app.myfiles.module.local.securelocal;

import android.content.Context;
import android.os.Bundle;

import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsReloadStrategyImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.provider.PrivateMyFilesProvider;
import com.sec.android.app.myfiles.util.StorageMonitor;
import com.sec.android.app.myfiles.util.UiUtils;

/**
 * Created by daesu83.kim on 2015-07-22.
 */
public class SecureLocalFileReloadStrategyImp extends AbsReloadStrategyImp {

    public SecureLocalFileReloadStrategyImp(Context context, NavigationInfo navigationInfo) {
        super(context, navigationInfo);
    }

    private boolean isValidRecord(FileRecord record) {
        boolean bRet = false;
        if (record != null) {
            if (record.exists(mContext)) {
                bRet = StorageMonitor.isStorageMounted(mContext, record.getFullPath());
            }
        }
        return bRet;
    }

    @Override
    public void reload(ReloadCause cause, Bundle extras, int event) {
        AbsMyFilesFragment curFragment = mNavigationInfo.getCurFragment();
        FileRecord curRecord = mNavigationInfo.getCurRecord();
        if (curFragment != null) {
            switch (cause) {
                case MediaScanStarted:
                    if (!curRecord.exists(mContext)) {
                        FileRecord parent = curRecord.getParent(mContext);
                        if (isValidRecord(parent)) {
                            NavigationInfo info = NavigationInfo.getInstance(NavigationInfo.NavigationMode.Normal, parent);
                            info.setScreenPath(UiUtils.getScreenPathFromRecord(parent));
                            NavigationManager.getInstance(curFragment.getProcessId()).enter(info);
                        } else if (StorageMonitor.isRemovedExtSDCard(curRecord.getFullPath())){
                            Log.d(this, "Removed SD Card path! So keep current path.");
                        } else {
                            MyFilesFacade.goHome(curFragment.getProcessId(), null, mContext);
                        }
                    }
                    break;
                case ContentChanged:
                    if (event == AppConstants.ObserverType.CURRENT_FOLDER) {
                        reloadRecord(curFragment.getProcessId(), curRecord);
                    } else {
                        FileRecord currentRecord = mNavigationInfo.getCurRecord();
                        PrivateMyFilesProvider.refreshDb(mContext, currentRecord, true);
                    }
                    break;
                case MediaScanFinished:
                    //reloading ejected USB storage causes abnormal behavior
                    if (curRecord.exists(mContext)) {
                        reloadRecord(curFragment.getProcessId(), curRecord);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private void reloadRecord(int processId, FileRecord record) {
        AbsMyFilesFragment curFragment = mNavigationInfo.getCurFragment();
        MyFilesFacade.reload(processId, mContext, record, curFragment);
    }
}
